/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao.util;

import java.util.List;
import model.pojo.ChiTietWsBanHang;
import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 *
 * @author LaiHuynh
 */
public class ChiTietWsBanHangDAO {

    public static List<ChiTietWsBanHang> layDanhSachWsBanHang() {
        List<ChiTietWsBanHang> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select ws from ChiTietWsBanHang ws";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }

    public static List<ChiTietWsBanHang> layDanhSachWsBanHangTheoDM(int maDanhMuc) {
        List<ChiTietWsBanHang> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select ws from ChiTietWsBanHang ws where ws.nhomWsBanHang.maDanhMuc = " + maDanhMuc;
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }

    public static List<ChiTietWsBanHang> LayDSPhanTrang(long slsach, long trang) {
        List<ChiTietWsBanHang> ds = null;
        org.hibernate.Session session = HibernateUtil.getSessionFactory().openSession();
        String hql;
        int n = ((int) trang - 1) * (int) slsach;
        int m = (int) slsach;
        try {
            if (slsach != -1) {
                hql = "select ws from ChiTietWsBanHang ws order by ws.maChiTiet";
                org.hibernate.Query query = session.createQuery(hql);
                query.setFirstResult(n);
                query.setMaxResults(m);
                ds = query.list();
            }
        } catch (HibernateException ex) {
            //Log the exception 
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }

    public static long SoLuongTin() {
        long sl = 0;
        org.hibernate.Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "select count(ws.maChiTiet) from ChiTietWsBanHang ws";
        org.hibernate.Query query = session.createQuery(hql);
        sl = (Long) query.uniqueResult();
        session.close();
        return sl;
    }

    public static long TinhSoTrang(long SLTinTrenTrang) {
        long sl = SoLuongTin();
        long s;
        if (SLTinTrenTrang != -1) {
            s = sl / SLTinTrenTrang;
            if (sl % SLTinTrenTrang != 0) {
                s++;
            }
        } else {
            s = 1;
        }
        return s;
    }

    //danh muc
    public static List<ChiTietWsBanHang> LayDSPhanTrangTheoDM(long slsach, long trang, int maDanhMuc) {
        List<ChiTietWsBanHang> ds = null;
        org.hibernate.Session session = HibernateUtil.getSessionFactory().openSession();
        String hql;
        int n = ((int) trang - 1) * (int) slsach;
        int m = (int) slsach;
        try {
            if (slsach != -1) {
                hql = "select ws from ChiTietWsBanHang ws where ws.nhomWsBanHang.maDanhMuc = " + maDanhMuc;
                org.hibernate.Query query = session.createQuery(hql);
                query.setFirstResult(n);
                query.setMaxResults(m);
                ds = query.list();
            }
        } catch (HibernateException ex) {
            //Log the exception 
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }

    public static long SoLuongTinTheoDM(int maDanhMuc) {
        long sl = 0;
        org.hibernate.Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "select count(ws.maChiTiet) from ChiTietWsBanHang ws where ws.nhomWsBanHang.maDanhMuc = " + maDanhMuc;
        org.hibernate.Query query = session.createQuery(hql);
        sl = (Long) query.uniqueResult();
        session.close();
        return sl;
    }

    public static long TinhSoTrangTheoDM(long SLTinTrenTrang, int maDanhMuc) {
        long sl = SoLuongTinTheoDM(maDanhMuc);
        long s;
        if (SLTinTrenTrang != -1) {
            s = sl / SLTinTrenTrang;
            if (sl % SLTinTrenTrang != 0) {
                s++;
            }
        } else {
            s = 1;
        }
        return s;
    }
}
